var showDictData;
layui.config({
    base : "/js/modules/"
}).extend({
    "bodyTab" : "bodyTab"
})
layui.use(['bodyTab','form','layer','table','admin', 'element'],function(){
    var form = layui.form;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var $ = layui.jquery;
    var element = layui.element;
    var table = layui.table;
    var admin = layui.admin;
    var tab = layui.bodyTab({
        openTabNum : "50"  //最大可打开窗口数量
    });
    var prefix = "system/dict";
    //列表
    var tableIns = table.render({
        elem: '#dictTypeList',
        url : ctx+prefix+'/listData',
        toolbar: '#toolbar',
        method: 'post',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,20,30],
        limit : 10,
        id : "dictTypeListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'dictName', title: '字典名称', minWidth:100, align:"center"},
            {field: 'dictType', title: '字典类型', minWidth:100, align:"center", templet: function (d) {
               return '<a data-url="/system/dict/datalist.html" style="color: blue" href="javascript:void(0);" onclick="showDictData(this)"><cite>'+d.dictType+'</cite></a>';
            }},
            {field: 'status', title: '状态（0正常 1停用）', minWidth:100, align:"center"},
            {field: 'createBy', title: '创建者', minWidth:100, align:"center"},
            {field: 'createTime', title: '创建时间', minWidth:100, align:"center"},
            {field: 'updateBy', title: '更新者', minWidth:100, align:"center"},
            {field: 'updateTime', title: '更新时间', minWidth:100, align:"center"},
            {field: 'remark', title: '备注', minWidth:100, align:"center"}
            ]],
        parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                // "msg": res.message, //解析提示文本
                "count": res.total, //解析数据长度
                "data": res.rows //解析数据列表
            };
        }
    });

    showDictData = function (abiaoq) {
        admin.putTempData('t_typeName', $(abiaoq).text());
        addTab1($(abiaoq));
    };
    var id = 0;
    //打开新窗口
    function addTab(_this){
        id++;
        element.tabAdd('bodyTab', {
            title: "字典数据",
            content: '<iframe src="/system/dict/datalist.html" name="iframe'+id+'" class="iframe" framborder="0" data-id="'+id+'" scrolling="auto" width="100%"  height="100%"></iframe>',
            id: new Date().getTime()
        });
        element.tabChange('bodyTab', id);
    }

    function addTab1(_this){
        tab.tabAdd(_this)
    }
    //搜索
    $(".search_btn").on("click",function(){
        table.reload("dictTypeListTable",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                searchKey: $(".searchKey").val()
            }
        })
    });

    //头工具栏事件
    table.on('toolbar(dictTypeList)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'add':
                console.log("add");
                showAddOrEditModel();
                break;
            case 'edit':
                console.log("edit");
                console.log("checkStatus:" + checkStatus);
                dataArr = checkStatus.data;
                if(dataArr.length > 0 && dataArr.length==1){
                    console.log(dataArr[0].postId);     //修改操作
                    var data = dataArr[0];
                    showAddOrEditModel(data);
                }
                else{
                    layer.msg("请选择一条数据!");
                }
                break;
            case 'del':
                console.log("del");
                console.log("checkStatus:" + checkStatus);
                data = checkStatus.data, inputs = [];
                if(data.length > 0) {
                    for (var i in data) {
                        inputs.push(data[i].roleId);
                    }
                    layer.confirm('确定删除选中的数据吗？', {icon: 3, title: '提示信息'}, function (index) {
                        console.log("inputs:"+inputs);
                        doDelete(inputs);//删除操作
                        layer.close(index);
                    })
                }else{
                    layer.msg("请选择需要删除的数据!");
                }
                break;
        };
    });

    //监听行工具事件
    table.on('tool(dictTypeList)', function (obj) {
        var data = obj.data;
        console.log("data:" + data)
        if (obj.event === 'del') {
            layer.confirm('真的删除行么?', function (index) {
                console.log("del");
                doDelete(data.roleId);//删除操作
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            showAddOrEditModel(data);
            console.log("edit");
        }
    });

    //新增或者修改岗位信息的弹框
    function showAddOrEditModel(data) {
        admin.putTempData('t_DictType', data);
        admin.putTempData('formOk', false);
        layer.open({
            type: 2,
            title: data ? '修改' : '添加',
            area: ['500px', '550px'],
            content: prefix+'/info.html',
            end: function () {
                console.log("end");
                admin.getTempData('formOk') && table.reload('dictTypeListTable');  // 成功刷新表格
            }
        });
    }

    //删除数据
    function doDelete(inputs){
        var ids = inputs.join(',');
        console.log("ids:"+ids);
        $.post(ctx+prefix+'/remove', {
            ids: ids
        }, function (data) {
            if (data.code == 0) {
                layer.msg(data.msg, {icon: 1});
                table.reload('dictTypeListTable');  // 成功刷新表格
            } else {
                layer.msg(data.msg, {icon: 2});
            }
        });
    }

})